// pages/content/content.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contentData: {
      selectX: 0,
      selectTagId: -1,
      cData: [],
      selectedKeys: [],
      tagData: [],
      loading: false,
      tabData: ['最新', '最热', '推荐'],
      selectTabIndex: 0,
      tabUnderLeft: 12,
      thingData: [],
      pageData: [],
      page: 1,
      total: 0,
      pageSize: 12
    }
  },

  onLoad: function () {
    this.initSider();
    this.getThingList({});
  },
  initSider: function () {
    let contentData = this.data.contentData;
    contentData.cData.push({ key: '-1', title: '全部' });
    this.setData({
      contentData: contentData
    });
    // 这里需要根据实际情况实现 API 请求，假设使用 wx.request
    wx.request({
      url: '/@/api/classification',
      success: (res) => {
        res.data.forEach(item => {
          item.key = item.id;
          contentData.cData.push(item);
        });
        this.setData({
          contentData: contentData
        });
      }
    });
    wx.request({
      url: '/@/api/tag',
      success: (res) => {
        contentData.tagData = res.data;
        this.setData({
          contentData: contentData
        });
      }
    });
  },
  getSelectedKey: function () {
    let contentData = this.data.contentData;
    if (contentData.selectedKeys.length > 0) {
      return contentData.selectedKeys[0];
    } else {
      return -1;
    }
  },
  getSelectedKey: function () {
    let contentData = this.data.contentData;
    if (contentData.selectedKeys.length > 0) {
      return contentData.selectedKeys[0];
    } else {
      return -1;
    }
  },
  clickTag: function (e) {
    let index = e.currentTarget.dataset.id;
    let contentData = this.data.contentData;
    contentData.selectedKeys = [];
    contentData.selectTagId = index;
    this.getThingList({ tag: contentData.selectTagId });
    this.setData({
      contentData: contentData
    });
  },
  selectTab: function (e) {
    let index = e.currentTarget.dataset.index;
    let contentData = this.data.contentData;
    contentData.selectTabIndex = index;
    contentData.tabUnderLeft = 12 + 50 * index;
    console.log(contentData.selectTabIndex);
    let sort = (index === 0 ? 'recent' : index === 1 ? 'hot' : 'recommend');
    let data = { sort: sort };
    if (contentData.selectTagId !== -1) {
      data['tag'] = contentData.selectTagId;
    } else {
      data['c'] = this.getSelectedKey();
    }
    this.getThingList(data);
    this.setData({
      contentData: contentData
    });
  },
  handleDetail: function (e) {
    let item = e.currentTarget.dataset.item;
    // 跳转新页面
    wx.navigateTo({
      url: `/pages/detail/detail?id=${item.id}`
    });
  },
  changePage: function (page) {
    let contentData = this.data.contentData;
    contentData.page = page;
    let start = (contentData.page - 1) * contentData.pageSize;
    contentData.pageData = contentData.thingData.slice(start, start + contentData.pageSize);
    console.log('第' + contentData.page + '页');
    this.setData({
      contentData: contentData
    });
  },
  getThingList: function (data) {
    let contentData = this.data.contentData;
    contentData.loading = true;
    this.setData({
      contentData: contentData
    });
    // // 这里需要根据实际情况实现 API 请求，假设使用 wx.request
    // wx.request({
    //   url: 'localhost:9100/api/thing',
    //   data: data,
    //   success: (res) => {
    //     contentData.loading = false;
    //     res.data.forEach((item, index) => {
    //       if (item.cover) {
    //         item.cover = 'BASE_URL/api/staticfiles/image/' + item.cover;
    //       }
    //     });
    //     console.log(res);
    //     contentData.thingData = res.data;
    //     contentData.total = contentData.thingData.length;
    //     this.changePage(1);
    //     this.setData({
    //       contentData: contentData
    //     });
    //   },
    //   fail: (err) => {
    //     console.log(err);
    //     contentData.loading = false;
    //     this.setData({
    //       contentData: contentData
    //     });
    //   }
    // });
  }

  /**
   * 生命周期函数--监听页面加载
   */
  // onLoad(options) {

  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  // onReady() {

  // },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow() {

  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  // onHide() {

  // },

  /**
   * 生命周期函数--监听页面卸载
   */
  // onUnload() {

  // },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh() {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom() {

  // },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})